package com.pb.b2b.common.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pb.b2b.common.dao.BaseDao;
import com.pb.b2b.common.exception.CustomException;
import com.pb.b2b.common.service.BaseService;

@Service(value = "baseServiceImpl")
public class BaseServiceImpl implements BaseService {
	private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);

	@Autowired
	private BaseDao baseDao;

	@Transactional
	public void flush() {
		try {
			baseDao.flush();
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public Serializable save(Object transientInstance) {
		Serializable serializable = null;
		try {
			serializable = baseDao.save(transientInstance);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return serializable;
	}

	@Transactional
	public void saveOrUpdate(Object transientInstance) {
		try {
			baseDao.saveOrUpdate(transientInstance);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public void delete(Object persistentInstance) {
		try {
			baseDao.delete(persistentInstance);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public void deleteAll(Class entityClass) {
		try {
			baseDao.deleteAll(entityClass);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public void deleteAll(Collection entities) {
		try {
			baseDao.deleteAll(entities);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public Object findById(Class entityClass, Serializable id) {
		Object object = null;
		try {
			object = baseDao.findById(entityClass, id);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return object;
	}

	@Transactional
	public List findByExample(Object instance) {
		List list = null;
		try {
			list = baseDao.findByExample(instance);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return list;
	}

	@Transactional
	public List findAll(Class entityClass) {
		List list = null;
		try {
			list = baseDao.findAll(entityClass);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return list;
	}

	@Transactional
	public List findByProperty(Class entityClass, String propertyName, Object value) {
		List list = null;
		try {
			list = baseDao.findByProperty(entityClass, propertyName, value);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return list;
	}

	@Transactional
	public Object findByPropertyUnique(Class entityClass, String propertyName, Object value) {
		Object object = null;
		try {
			object = baseDao.findByPropertyUnique(entityClass, propertyName, value);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return object;
	}

	@Transactional
	public List findByIds(Class claz, List<Serializable> idList) {
		List list = null;
		try {
			list = baseDao.findByIds(claz, idList);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return list;
	}

	@Transactional
	public void setBaseDao(BaseDao baseDao) {
		try {
			this.baseDao = baseDao;
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public List findByProperties(Class entityClass, String[] propertyNames, Object[] values) {
		List list = null;
		try {
			list = baseDao.findByProperties(entityClass, propertyNames, values);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
		return list;
	}

	@Transactional
	public void evict(Object objToEvict) {
		try {
			baseDao.evict(objToEvict);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}

	@Transactional
	public void merge(Object obj) {
		try {
			baseDao.merge(obj);
		} catch (Exception e) {
			new CustomException(e, BaseServiceImpl.class);
		}
	}
}